package org.ifilm.service;

import java.util.ArrayList;
import java.util.List;

import org.ifilm.dao.NoticeDao;
import org.ifilm.model.EquipmentOffer;
import org.ifilm.model.EquipmentPosting;
import org.ifilm.model.JobApplication;
import org.ifilm.model.Project;
import org.ifilm.model.UserJobPosting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Notice Service implementation
 * 
 * @author Mustafa Genc
 *
 */
@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {
	@Autowired
	private NoticeDao noticeDao;

	@Override
	public UserJobPosting createJobPosting(UserJobPosting jobPosting) {
		return noticeDao.createJobPosting(jobPosting);
	}
	
	@Override 
	public void editJobPosting(UserJobPosting jobPosting){
		noticeDao.editJobPosting(jobPosting);
	}
	@Override
	public EquipmentPosting createEquipmentPosting(EquipmentPosting equipmentPosting) {
		return noticeDao.createEquipmentPosting(equipmentPosting);
	}

	@Override
	public List<UserJobPosting> listJobPostings() {
		return noticeDao.listJobPostings();
	}

	@Override
	public void updateProjectPostings(Project project, List<UserJobPosting> userJobPostings, List<EquipmentPosting> equipmentPostings) {
		if (userJobPostings != null) {
			List<UserJobPosting> newAndOldPostings = new ArrayList<UserJobPosting>(userJobPostings);
			newAndOldPostings.addAll(project.getJobPostings());
			project.setJobPostings(newAndOldPostings);
		}
		
		if (equipmentPostings != null) {
			List<EquipmentPosting> newAndOldPostings = new ArrayList<EquipmentPosting>(equipmentPostings);
			newAndOldPostings.addAll(project.getEquipmentPostings());
			project.setEquipmentPostings(newAndOldPostings);
		}
	}

	@Override
	public UserJobPosting getUserJobPosting(Long id) {
		return noticeDao.getUserJobPosting(id);
	}

	@Override
	public EquipmentPosting getUserEquipmentPosting(Long id) {
		return noticeDao.getUserEquipmentPosting(id);
	}

	@Override
	public JobApplication createJobApplication(JobApplication jobApplication) {
		return noticeDao.createJobApplication(jobApplication);
	}

	@Override
	public EquipmentOffer createEquipmentOffer(EquipmentOffer equipmentOffer) {
		return noticeDao.createEquipmentOffer(equipmentOffer);
	}

	@Override
	public void updateJobPostingAppliances(UserJobPosting userJobPosting, JobApplication jobApplication) {
		userJobPosting.getJobApplications().add(jobApplication);
	}

	@Override
	public void updateEquipmentOffers(EquipmentPosting equipmentPosting, EquipmentOffer equipmentOffer) {
		equipmentPosting.getEquipmentOffers().add(equipmentOffer);
	}

	@Override
	public List<JobApplication> getJobApplications(Long userId) {
		return noticeDao.getJobApplications(userId);
	}
	
	@Override
	public List<EquipmentOffer> getEquipmentOffers(Long userId) {
		return noticeDao.getEquipmentOffers(userId);
	}
	
	@Override
	public JobApplication getJobApplicationById(Long jobApplicationId) {
		return noticeDao.getJobApplicationById(jobApplicationId);
	}
	
	@Override
	public void updateJobApplication(JobApplication jobApplication) {
		noticeDao.updateJobApplication(jobApplication);
	}

	@Override
	public void deleteJobPosting(UserJobPosting posting) {
		posting.getProject().getJobPostings().remove(posting);
		
		noticeDao.deleteJobPosting(posting.getId());
	}
}